package com.xwamp.webutil;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

/**
 * 输出工具类
 *
 * @author SoChishun
 * @version 1.0 2017-5-13
 *
 */
public class ResponseUtil {

    public static void write(HttpServletResponse response, String text) {
        write(response, text, "utf-8");
    }

    public static void write(HttpServletResponse response, String text, String charsetEncoding) {
        if (null == response) {
            return;
        }
        try {
            response.setCharacterEncoding(charsetEncoding);
            response.getWriter().write(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置Excel等文件在线下载
     *
     * @param response
     * @param fileName
     * @since 1.0 2017-5-26
     */
    public static void setPoiDowloadHeader(HttpServletResponse response, String fileName) {
        // 清空response  
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
        //response.addHeader("Content-Length", "" + file.length()); 
        //response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
        response.setContentType("application/x-msdownload"); // "application/msexcel;charset=" + getEncoding();
    }
}
